//封装axios
import axios from 'axios';

//跨域请求
axios.defaults.withCredentials = true
//请求头
//axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
//请求地址
axios.defaults.baseURL="http://localhost:8001"
//请求时间
axios.defaults.timeout=5000
//响应拦截器
axios.interceptors.response.use(response=>{
    console.log(response)
		if(response.status === 200){
			//Promise.resolve(response)登录成功
			return Promise.resolve(response);
		}else{

			return Promise.reject(response);
		}
	},
	err=>{
    console.log(err)
		if(err.response.status){
			switch(err.response.status){
				case 401:
					router.replace({
						path:"/",
						query:{
							//在登录成功后返回当前页面，这一步需要在登录页操作。
							redirect: router.currentRoute.fullPath
						}
					})
					break
				case 404:
					break
			}
			//返回参数
			return Promise.reject(err.response);
		}


	})

export function get(URL,params={}){
	return new Promise((resolve,reject)=>{
		axios.get(URL,{params:params})
		.then(res=>{
			resolve(res.data)
		})
		.catch(err=>{
			reject(err)
		})
	})
}

export function del(URL,params={}){
	return new Promise((resolve,reject)=>{
		axios.delete(URL,{params:params})
		.then(res=>{
			resolve(res.data)
		})
		.catch(err=>{
			reject(err.data)
		})
	})
}
export function put(URL,data){
	return new Promise((resolve,reject)=>{
		axios.put(URL,data)
		.then(res=>{
			resolve(res.data)
		})
		.catch(err=>{
			reject(err.data)
		})
	})
}
export function post(url, params) {
    return new Promise((resolve, reject) => {
         axios.post(url,params)
        .then(res => {
            resolve(res.data);
        })
        .catch(err =>{
            reject(err.data)
        })
    });
}
/* export function post(URL,data={}){
	return new Promise((resolve,reject)=>{
		axios.post(URL,data)
		.then(res=>{
			resolve(res)
		})
		.catch(err=>{
			reject(err)
		})
	})
} */
